<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../../common/Common.php");
require_once(dirname(__FILE__) . "/../../dbcom/ServiceCom.php");

$usercom = new UserCom();
$ragioncom = new RagionCom();
$common = new Common();
$servicecom = new ServiceCom();

$userid = $common->getLogin();
if (!isset($userid) || empty($userid)) {
    header('Location:./../login.php');
}
//获取分页
if (empty($_REQUEST['p']) || !isset($_REQUEST['p'])) {
    $page = 1;
    $minlimit = 0;
} else {
    $page = $_REQUEST['p'];
    if ($page > 1) {
        $minlimit = 50 * $page;
    } else {
        $minlimit = 0;
    }
}
$limitnum = 50;

$servicearr = $servicecom->getServicelist($minlimit, $limitnum);
$number = $servicecom->getAllNum();

$p = (int) ($number / 50 + 1);

$res = array();
if (!empty($servicearr)) {
    foreach ($servicearr as $k => $v) {
        $res[$k]['id'] = $v['id'];
        $res[$k]['name'] = $v['name'];
        $res[$k]['fee'] = sprintf("%.2f", $v['fee'] / 100);
        $res[$k]['countfee'] = sprintf("%.2f", $v['countfee'] / 100);
    }
}
?>
<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="keywords" content="">
        <meta name="description" content="">
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">

        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">

        <link href="../public/css/paging.css" rel="stylesheet">
        <link href="../public/css/showproject.css" rel="stylesheet">
    </head>

    <body class="gray-bg">

        <div class="wrapper wrapper-content animated fadeInUp">
            <div class="row">
                <div class="col-sm-12">

                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>服务管理</h5>
                            <div class="ibox-tools">
                                <a id="addnew" data-toggle="modal" data-target="#myModal1" class="btn btn-primary btn-xs">新增</a>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <div class="row m-b-sm m-t-sm">
                                <div class="col-md-1">

                                </div>
                                <div class="col-md-11">
                                    <div class="input-group">

                                    </div>
                                </div>
                            </div>

                            <div class="project-list">

                                <div class="project-list">
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <td class="project-title">
                                                        <span>名称</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>单价(CNY)</span>
                                                    </td>
                                                    <td class="project-actions">
                                                        <span>操作</span>
                                                    </td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                foreach ($res as $v) {
                                                    ?>
                                                    <tr id="list<?= $v['id'] ?>" data-id="<?= $v['id'] ?>">
                                                        <td class="project-title">
                                                            <span><?= $v['name'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['fee'] ?></span>
                                                        </td>
                                                        <td class="project-actions">
                                                            <a data-id="<?= $v['id'] ?>" data-toggle="modal" data-target="#myModal2" class="btn btn-white btn-sm btn-edt"><i class="fa fa-folder"></i> 编辑 </a>
                                                            <a data-id="<?= $v['id'] ?>" class="btn btn-white btn-sm btn-det"><i class="fa fa-folder"></i> 删除 </a>
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                        <div class="box" id="box"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" id="close1" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">新增服务</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>服务名称</label>
                            <input type="text" placeholder="" id="newname" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>单价(CNY)</label>
                            <input type="text" placeholder="" id="newfee" class="form-control">
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否属于记次卡</label>

                            <div class="col-sm-10">
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="1" id="isncard">记次卡</label>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="addservice" class="btn btn-info">保存</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>

        <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <input type="hidden" value="" id="serviceid"/>
                    <div class="modal-header">
                        <button type="button" class="close" id="close1" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">服务</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>服务名称</label>
                            <input type="text" placeholder="" id="name" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>单价(CNY)</label>
                            <input type="text" placeholder="" id="fee" class="form-control">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="mychange" class="btn btn-info">修改</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>

        <!-- 全局js -->
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>

        <!-- 自定义js -->
        <script src="../public/js/paging.js?v=1.0.0"></script>
        <script src="../public/js/upload.js?v=1.0.0"></script>
        <script src="../public/js/common.js?v=1.0.0"></script>

        <script>
            $(document).ready(function () {

                $('#loading-example-btn').click(function () {
                    btn = $(this);
                    simpleLoad(btn, true)

                    // Ajax example
                    //                $.ajax().always(function () {
                    //                    simpleLoad($(this), false)
                    //                });

                    simpleLoad(btn, false)
                });
            });

            function simpleLoad(btn, state) {
                if (state) {
                    btn.children().addClass('fa-spin');
                    btn.contents().last().replaceWith(" Loading");
                } else {
                    setTimeout(function () {
                        btn.children().removeClass('fa-spin');
                        btn.contents().last().replaceWith(" Refresh");
                    }, 2000);
                }
            }
            var setTotalCount = <?= $number ?>;
            $('#box').paging({
                initPageNo: <?= $page ?>, // 初始页码
                totalPages: <?= $p ?>, //总页数
                totalCount: '合计' + setTotalCount + '条数据', // 条目总数
                slideSpeed: 600, // 缓动速度。单位毫秒
                jump: true, //是否支持跳转
                callback: function (page) { // 回调函数
                    console.log(page);
                }
            });

            $(".btn-det").click(function () {
                var serviceid = $(this).data("id");
                var datastr = "cont=4&serviceid=" + serviceid;
                $.ajax({
                    url: "/../../controller/service.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        //console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            layer.msg('删除成功', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            $("#list" + result.data).css("display", "none");
                        } else {
                            layer.msg('删除失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
            $("#addservice").click(function () {
                //console.log('start save');
                var name = $("#newname").val();
                var fee = $("#newfee").val();
                var countfee = $("#newcountfee").val();
                if ($("#isncard").is(':checked')) {
                    var isncard = 1;
                } else {
                    var isncard = 2;
                }
                if (name == "" || fee == "" || countfee == "") {
                    layer.msg('请填写必要项', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                var datastr = "cont=1&name=" + name + "&fee=" + fee + "&countfee=" + countfee + "&isncard=" + isncard;
                $.ajax({
                    url: "/../../controller/service.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            window.location.reload();
                        } else {
                            layer.msg('添加失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                    }
                });
            });

            $(".btn-edt").click(function () {
                var serviceid = $(this).data("id");
                $("#serviceid").val(serviceid);
                var datastr = "cont=2&serviceid=" + serviceid;
                $.ajax({
                    url: "/../../controller/service.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            $("#name").val(result.data.name);
                            $("#fee").val(result.data.fee);
                            $("#countfee").val(result.data.countfee);
                        } else {
                            layer.msg('未找到该服务请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                    }
                });
            });

            $("#mychange").click(function () {
                var serviceid = $("#serviceid").val();
                var name = $("#name").val();
                var fee = $("#fee").val();
                var countfee = $("#countfee").val();
                if (serviceid == "") {
                    layer.msg('未找到该服务请刷新重试', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                if (name == "" || fee == "") {
                    layer.msg('请填写内容', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                var datastr = "cont=3&name=" + name + "&serviceid=" + serviceid + "&fee=" + fee + "&countfee=" + countfee;
                $.ajax({
                    url: "/../../controller/service.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            window.location.reload();
                        } else {
                            layer.msg('更新失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                    }
                });
            });

        </script>

    </body>
</html>
